#include<iostream>
#include<unistd.h>
#include<cstdlib>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<cstdio>
using namespace std;

int main()
{
  pid_t id = fork();
  if(id < 0)
  {
    cout<<"errno:"<<errno<<"strerror:"<<strerror(errno)<<endl;
  }
  else if(id == 0)
  {
    int cnt = 5;
    while(cnt)
    {
      cout<<"我是一个子进程,pid:"<<getpid()<<endl;
      cnt--;
      sleep(1);
    }
    exit(1);
  }
  else
  {
    sleep(6);
    int status = 0;
    pid_t rid = waitpid(id,&status,0);
    if(rid>0)
    {
      cout<<"wait sub process success,rid:"<<rid<<"status code:"<<((status>>8)&0xFF)<<endl;
    }
    else
    {
      perror("waitpid");
    }
  }

  return 0;
}
